package com.hxm.meizi.provider;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hxm.meizi.R;
import com.hxm.meizi.entity.ImageUrl;
import com.hxm.meizi.ui.activity.ImagePreviewActivity;
import com.hxm.meizi.util.ImageLoader;

import java.util.ArrayList;

import me.drakeet.multitype.ItemViewProvider;
import me.drakeet.multitype.Items;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by hxm on 2017/2/19.
 */

public class ImageListProvider extends ItemViewProvider<ImageUrl, ImageListProvider.ImageListHolder> {

    private Items items;

    public ImageListProvider(Items items) {
        this.items = items;
    }

    @NonNull
    @Override
    protected ImageListHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_image_list_provider, parent, false);
        return new ImageListHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ImageListHolder holder, @NonNull final ImageUrl imageUrl) {
        final Context context = holder.itemView.getContext();
        final ImageView imageView = holder.imageView;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ArrayList<ImageUrl> list = new ArrayList<>();
                Observable.from(items)
                        .map(new Func1<Object, ImageUrl>() {
                            @Override
                            public ImageUrl call(Object o) {
                                return (ImageUrl) o;
                            }
                        })
                        .subscribe(new Action1<ImageUrl>() {
                            @Override
                            public void call(ImageUrl imageUrl) {
                                list.add(imageUrl);
                            }
                        });
                Intent intent = new Intent(context, ImagePreviewActivity.class);
                intent.putParcelableArrayListExtra("item_data", list);
                intent.putExtra("currentPosition", holder.getAdapterPosition());
                context.startActivity(intent);
            }
        });

        ImageLoader.display(context, imageUrl.getUrl(), holder.imageView, holder.loading, holder.loadingError);
    }

    static class ImageListHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private LinearLayout loading, loadingError;

        public ImageListHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            loading = (LinearLayout) itemView.findViewById(R.id.loading);
            loadingError = (LinearLayout) itemView.findViewById(R.id.loading_error);
        }
    }
}
